(function ($) {
    var grid_setting = {
        singleSelect: true,
        margin: 'center' // left,center
//      selectOnCheck: true,
//      sortName: [],
//      sortOrder: 'asc'
    }

    parseOptions = function (o) {
        var option = {};
        var s = $.trim(o);
        if (s) {
            if (s.substring(0, 1) !== "{") {
                s = "{" + s + "}";
            }
            option = (new Function("return " + s))();
        }
        return option;
    };

    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    $.fn.grid = function (option) {
        var dg = $(this);
        var settings = {};
        var controlOptions = self.parseOptions(dg.data('options'));

        $.extend(true, settings, grid_setting, option, controlOptions || {});
        settings.margin === 'center' && dg.css({
            marginLeft: 'auto',
            marginRight: 'auto'
        });
        dg.find('tr:eq(0) th') && dg.find('tr:eq(0)').addClass('thead');

        var Request = GetRequest();
        if (Request['order']) {
            dg.find('.thead').find('th[data-order=' + Request['order'] + ']').addClass(Request['sort']).attr('data-sort', Request['sort']).append('<span></span>');
        }
        dg.find('.thead').find('th[data-order]').each(function () {
            $(this).addClass('GridSortTh').click(function () {
                var data = $(this).data();
                Request['order'] = data['order'];
                if (data['sort'] === 'desc') {
                    Request['sort'] = 'asc';
                } else {
                    Request['sort'] = 'desc';
                }
                var $url = '?';
                for (var i in Request) {
                    $url += i + '=' + Request[i] + '&';
                }
                $url = $url.substr(0, $url.length - 1);
                window.location.href = $url;
            });
        });

        dg.on('click', 'tr:not(.thead)', function (e) {
            if (settings.singleSelect) {
                dg.find('tr').not($(this)).removeClass('selected');
                dg.find('tr').not($(this)).find('td:eq(0) :checkbox').prop('checked', false);
            }
            var _c = $(this).find('td:eq(0) :checkbox');
            if (e.target.type !== 'checkbox') {
                _c.prop('checked', !_c.prop('checked'));
            }
            $(this).toggleClass('selected');
        });


        dg.on('click', 'tr.thead :checkbox', function () {
            var _c = dg.find('tr').find('td:eq(0) :checkbox');
            if (settings.singleSelect) {
                dg.find('tr').removeClass('selected');
                _c.prop('checked', false);
            } else {
                dg.find('tr').toggleClass('selected', $(this).prop('checked'));
                _c.prop('checked', $(this).prop('checked'));
            }
        });

    };

})(jQuery);
